$.fn.dataTable.ext.errMode = 'none';

$(function() {
	var orderTable;
	var innerID;
	var url = ctxPath + "/sys/user/getUser.do";
	var data = {};
	doGetReq(url, data, function(rsp) {
		var retData = rsp.retData;
		innerID = retData.innerID;
		$("#queryData").trigger("click");
	});
	
    $('.input-daterange input').datepicker({
        format: "yyyy-mm-dd",
        clearBtn: true,
        language: "zh-CN",
        autoclose: true,
        endDate: "0d"
    });
	
	$("#queryData").bind("click", function() {
		orderTable = initOrderTable(innerID);
		querySum(innerID);
	});
	
	$("#exportExecl").bind("click", function() {
		exportExecl(innerID);
	});
	
	$("#orderTable tbody").on("click",".edit-btn",function(){
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
            var jqob=$(val);
            if(i == 3) {
            	var txt=jqob.text();
            	var put=$("<input type='text' />");
            	put.val(txt);
            	jqob.html(put);
            }
        });
        $(this).html("保存");
        $(this).toggleClass("edit-btn");
        $(this).toggleClass("save-btn");
    });
	
	$("#orderTable tbody").on("click",".save-btn",function(){
        var row=orderTable.row($(this).parents("tr"));
        var tds=$(this).parents("tr").children();
        $.each(tds, function(i,val){
        	var jqob=$(val);
        	//把input变为字符串
        	if(i == 3) {
        		var txt=jqob.children("input").val();
        		jqob.html(txt);
        		orderTable.cell(jqob).data(txt);//修改DataTables对象的数据
        	}
        });
        var orderID = orderTable.row($(this).parents("tr")).data().orderID;
        var orderPrice = orderTable.row($(this).parents("tr")).data().orderPrice;
        $.ajax({
            "url": ctxPath + "/sys/order/updateOrder.do",
            "data":{"orderPrice": orderPrice, "orderID": orderID},
            "type":"post",
            "success":function(response){
                alert("修改成功");
            }
        });   
        $(this).html("编辑");
        $(this).toggleClass("edit-btn");
        $(this).toggleClass("save-btn");
    });
});

function initOrderTable(innerID) {
	return $("#orderTable").DataTable({
        "bProcessing": true,
        "bServerSide": true,
        "bDestroy" : true,
        "bFilter": false,
        "bLengthChange": false,
        "bAutoWidth" : true,
		"sAjaxDataProp" : "aaData",
		"bSort" : false,
		"sAjaxSource" : ctxPath + "/sys/order/queryOrder.do",
        "aoColumns": [
			{"mData": "id", "bVisible": false},
			{"mData": "orderID", "bVisible": false},
			{"mData": "activity"},
			{"mData": function(order) {
				var productName = order.productName;
				var num = productName.substr(productName.indexOf("[") + 1, 10);
				var name = productName.substr(productName.indexOf("]") + 1);
				return "<a href='https://www.amazon.co.jp/gp/product/"+num+"/?smid=AN1VRQENFRJN5' target='_blank'>"+name+"</a>";
				
			}},
			{"mData": "price"},
			{"mData": "rebate"},
			{"mData": "count"},
			{"mData": "orderDate"},
			{"mData": function(order) {
				var isValid = order.isValid;
				if("2"==isValid) {
					return "有效";
				} else{
					return "待审核";
				}
			}},
			{"mData": function(order) {
				var isRebate = order.isRebate;
				if("2"==isRebate) {
					return "已发放";
				} else {
					return "未发放";
				}
			}}],
		"fnServerParams": function(aoData) {
			if (innerID != undefined) {
				aoData.push({
					"name" : "innerID",
					"value" : innerID
				});
			}
			var shopSite = $("#shopSite").val();
			if(shopSite != "") {
				aoData.push({
					"name" : "shopSite",
					"value" : shopSite
				});
			}
			var orderID = $("#orderID").val();
			if(orderID != "") {
				aoData.push({
					"name" : "orderID",
					"value" : orderID
				});
			}
			var startDate = $("#startDate").val();
			if(startDate != "") {
				aoData.push({
					"name" : "startDate",
					"value" : startDate
				});
			}
			var endDate = $("#endDate").val();
			if(endDate != "") {
				aoData.push({
					"name" : "endDate",
					"value" : endDate
				});
			}
			var isValid = $("#isValid").val();
			if(isValid != "") {
				aoData.push({
					"name" : "isValid",
					"value" : isValid
				});
			}
			var isRebate = $("#isRebate").val();
			if(isRebate != "") {
				aoData.push({
					"name" : "isRebate",
					"value" : isRebate
				});
			}
		},
		"fnServerData": function(sSource, aoData, fnCallback) {
			$.ajax({
				"dataType": 'json',
				"type": "POST",
				"url": sSource,
				"data": aoData,
				"success": function(resp) {
					var retCode = resp.retCode;
					if (retCode == "SUCCESS") {
						fnCallback(resp.retData);
					}
				}
			});
		},
		"fnDrawCallback": function(table) {  
            $("#orderTable_paginate").append("  到第 <input style='height:28px;line-height:28px;width:40px;' class='margin text-center' id='changePage' type='text'> 页  <a class='btn btn-default shiny' style='margin-bottom:5px' href='javascript:void(0);' id='dataTable-btn'>确认</a>");  
            var oTable = $("#orderTable").dataTable();  
            $('#dataTable-btn').click(function(e) {  
                if($("#changePage").val() && $("#changePage").val() > 0) {  
                    var redirectpage = $("#changePage").val() - 1;  
                } else {  
                    var redirectpage = 0;  
                }  
                oTable.fnPageChange(redirectpage);  
            });  
        }
	});
}

function querySum(innerID) {
	var url = ctxPath + "/sys/order/querySum.do";
	var orderID = $("#orderID").val();
	var shopSite = $("#shopSite").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var isValid = $("#isValid").val();
	var isRebate = $("#isRebate").val();
	var data = new Object();
	data.innerID = innerID;
	if(orderID != "") {
		data.orderID = orderID;
	}
	if(shopSite != "") {
		data.shopSite = shopSite;
	}
	if(startDate != "") {
		data.startDate = startDate;
	}
	if(endDate != "") {
		data.endDate = endDate;
	}
	if(isValid != "") {
		data.isValid = isValid;
	}
	if(isRebate != "") {
		data.isRebate = isRebate;
	}
	doPostReq(url, data, function(rsp) {
		$("#querySum").html("总佣金：" + rsp.retData.rebate + "元");
	});
}

//数据导出
function exportExecl(innerID) {
	// 创建Form  
    var form = $('<form>');  
    // 设置属性                       
    var url = ctxPath + '/sys/order/exportOrderData.do';
    form.attr("style","display:none");
    form.attr('target', '');
    form.attr('method', 'post');  
    
    // 创建Input  
    var innerID_input = $('<input type="hidden" name="innerID" />');  
    innerID_input.attr('value', innerID);
	form.append(innerID_input);  
    // 创建Input  
    var orderID_input = $('<input type="hidden" name="orderID" />');  
    var orderID = $("#orderID").val();
    if(orderID != "") {
    	orderID_input.attr('value', orderID);
    	form.append(orderID_input);  
    }
    // 创建Input  
    var shopSite_input = $('<input type="hidden" name="shopSite" />');  
    var shopSite = $("#shopSite").val();
    if(shopSite != "") {
    	shopSite_input.attr('value', shopSite);
    	form.append(shopSite_input);  
    }
    // 创建Input  
    var startDate_input = $('<input type="hidden" name="startDate" />');  
    var startDate = $("#startDate").val();
    if(startDate != "") {
    	startDate_input.attr('value', startDate);  
    	form.append(startDate_input); 
    }
    // 创建Input  
    var endDate_input = $('<input type="hidden" name="endDate" />');  
    var endDate = $("#endDate").val();
    if(endDate != "") {
    	endDate_input.attr('value', endDate);  
    	form.append(endDate_input); 
    }
    // 创建Input  
    var isValid_input = $('<input type="hidden" name="isValid" />');  
    var isValid = $("#isValid").val();
    if(isValid != "") {
    	isValid_input.attr('value', isValid);  
    	form.append(isValid_input); 
    }
    // 创建Input  
    var isRebate_input = $('<input type="hidden" name="isRebate" />');  
    var isRebate = $("#isRebate").val();
    if(isRebate != "") {
    	isRebate_input.attr('value', isRebate);  
    	form.append(isRebate_input); 
    }
    form.attr('action', url);
    $('body').append(form);
    // 提交表单  
    form.submit();
}